package com.example.gateway;

import com.example.gateway.handler.HystrixFallbackHandler;
import com.example.gateway.handler.TestHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;

/**
 * @author lengleng
 * @date 2019/2/1
 * 路由配置信息
 */
@Slf4j
@Configuration
@AllArgsConstructor
@EnableWebFlux
public class RouterFunctionConfiguration implements WebFluxConfigurer {
	private final HystrixFallbackHandler hystrixFallbackHandler;
	private final TestHandler testHandler;


	@Bean
	public RouterFunction routerFunction() {
		return RouterFunctions
				.route(RequestPredicates.path("/fallback")
						.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler)
				.andRoute(RequestPredicates.POST("/test")
						.and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), testHandler::test);
	}

}
